package com.example.backend.controller;

import com.example.backend.entity.StorageOut;
import com.example.backend.service.StorageOutService;
import com.example.backend.util.ApiResponse;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/api/storage-out")
public class StorageOutController {
    @Autowired
    private StorageOutService storageOutService;

    @PostMapping
    public ApiResponse<String> addStorageOut(@RequestBody StorageOut storageOut) {
        storageOutService.addStorageOut(storageOut);
        return ApiResponse.success("出库成功");
    }

    @GetMapping("/{id}")
    public ApiResponse<StorageOut> getStorageOutById(@PathVariable("id") Long id) {
        StorageOut storageOut = storageOutService.getStorageOutById(id);
        if (storageOut != null) {
            return ApiResponse.success(storageOut);
        }
        return ApiResponse.error(404, "出库记录不存在");
    }

    @GetMapping
    public ApiResponse<List<StorageOut>> getAllStorageOut() {
        return ApiResponse.success(storageOutService.getAllStorageOut());
    }

    @PutMapping
    public ApiResponse<String> updateStorageOut(@RequestBody StorageOut storageOut) {
        storageOutService.updateStorageOut(storageOut);
        return ApiResponse.success("更新成功");
    }

    @DeleteMapping("/{id}")
    public ApiResponse<String> deleteStorageOut(@PathVariable("id") Long id) {
        storageOutService.deleteStorageOut(id);
        return ApiResponse.success("删除成功");
    }

    @GetMapping("/order/{orderNumber}")
    public ApiResponse<List<StorageOut>> getByOrderNumber(@PathVariable("orderNumber") String orderNumber) {
        return ApiResponse.success(storageOutService.getByOrderNumber(orderNumber));
    }

    @GetMapping("/port/{portId}")
    public ApiResponse<List<StorageOut>> getStorageOutByPortId(@PathVariable("portId") Long portId) {
        List<StorageOut> storageOuts = storageOutService.getStorageOutByPortId(portId);
        return ApiResponse.success(storageOuts);
    }

    @PostMapping("/batch")
    public ApiResponse<Integer> batchAddStorageOut(@RequestBody List<StorageOut> storageOutList) {
        int successCount = storageOutService.batchAddStorageOut(storageOutList);
        return ApiResponse.success(successCount);
    }

    @PostMapping("/clear")
    public ApiResponse<Integer> clearAll() {
        int count = storageOutService.deleteAll();
        return ApiResponse.success(count);
    }
} 